package util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import fileinput.ExDomainRegex;
import fileinput.Pair;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Tuple;

public class BaseRedisDao {

	private  final int REDIS_CONNECT_LATENCY = 1000000;
	private  final int REDIS_MAXACTIVE = 300;
	private  final String REDIS_KEY_DELIMIT = ":";
	//网站信息的rediskey
	private  final String TOP_DOMAIN_REDIS_KEY = "BR2005";
	// 泛域名的redis Key
	private  final String EX_DOMAIN_REDIS_KEY = "EXDOMAIN";
	// 泛域名后缀  字段名
	private  final String EX_DOMAIN_SUFFIX_KEY = "suffix";
	// 泛域名转化为的域名  字段名
	private  final String EX_DOMAIN_NAME_KEY = "domain";
	// 泛域名对应的IPS  字段名
	private  final String EX_DOMAIN_IPS_KEY = "ips";
		
	private  final String EX_DOMAIN_DEFAULT_IP = "0.0.0.0";
	private  int  maxLimit =10000;
	//private static  BaseRedisDao redisDao = null;
	private  Map<String, JedisPool> jedisPoolMap = new ConcurrentHashMap<String, JedisPool>();
	// format: keyprefix$host1|keyprefix$host2
	private  String serversInfo = null;
	private  Log LOG = LogFactory.getLog(BaseRedisDao.class);
	private Pattern keyPattern = Pattern.compile("(rzx:massdata:dnsstat:lock:|BR2005|2|ipcheck)");

	public BaseRedisDao() {
		try {
			maxLimit =Integer.parseInt(ConfigUtil.get("surfilter.massdata.exdomain.max.limit","10000"));
		} catch (Exception e) {
			LOG.error("init parameters error..", e);
		}
		
		if (serversInfo == null)
			getRedisServers();
//		BR2005$172.31.25.8:6379$|
//		rzx:massdata2:dnsstat:lock:$172.31.25.132:6379$|
//		2$172.31.25.8:6379$|
//		ipcheck$172.31.25.132:6379$
		String[] splits = serversInfo.split("\\|");
		for (String str : splits) {
			String[] sps = str.split("\\$");
			//sps[0]=BR2005
			//sps[1]=172.31.25.8:6379
			if (sps.length < 2) {
				LOG.error("serversInfo error: " + str);
				continue;
			}
			String keyPrefix = sps[0];
			String[] inSps = sps[1].split(REDIS_KEY_DELIMIT);
			//172.31.25.8:6379
//			inSps[0]=172.31.25.8
//			inSps[1]=6379
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxActive(REDIS_MAXACTIVE);
			// 在borrow一个jedis实例时，是否提前进行alidate操作；如果为true，则得到的jedis实例均是可用的；
			jedisPoolConfig.setTestOnBorrow(false);
			// 在还会给pool时，是否提前进行validate操作
			jedisPoolConfig.setTestOnReturn(false);
			JedisPool jedisPool;
			if (sps.length == 3) {
				LOG.info("password is " + sps[2]);

				jedisPool = new JedisPool(jedisPoolConfig, inSps[0], Integer.parseInt(inSps[1]), REDIS_CONNECT_LATENCY,
						sps[2]);
				
			} else {
				LOG.info("no password");
				jedisPool = new JedisPool(jedisPoolConfig, inSps[0], Integer.parseInt(inSps[1]), REDIS_CONNECT_LATENCY);

			}
			if(jedisPool!=null){
				System.out.println("keyPrefix:"+keyPrefix);
			}
			
			jedisPoolMap.put(keyPrefix, jedisPool);
		}
	}

	/*public static BaseRedisDao getInstance() {
		if (redisDao == null) {
			redisDao = new BaseRedisDao();
		}
		return redisDao;
	}*/

	public void getRedisServers() {
		try {
			serversInfo = ConfigUtil.get("surfilter.massdata.redis");
		} catch (Exception e) {
			LOG.error("parameter miss:surfilter.massdata.redis" , e);
		}
	}

	public String getKeyPrefix(String key) {
		if (keyPattern != null) {
			Matcher m = keyPattern.matcher(key);
			if (m.find())
				return m.group(1);
		}
		return null;
	}

	public JedisPool getJedisPool(String key) {
		String keyPrefix = getKeyPrefix(key);
		if (keyPrefix == null) {
			LOG.error("key is illegal, can not get jedisPool: " + key);
			return null;
		}
		return jedisPoolMap.get(keyPrefix);
	}

	public Pair<Pipeline, Pair<JedisPool, Jedis>> getPipeline(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		return new Pair<Pipeline, Pair<JedisPool, Jedis>>(jedis.pipelined(), new Pair<JedisPool, Jedis>(pool, jedis));
	}

	public Long persist(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.persist(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String set(String key, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.set(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String get(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.get(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Boolean exists(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.exists(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String type(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.type(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long expire(String key, int seconds) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.expire(key, seconds);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long expireAt(String key, long unixTime) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.expireAt(key, unixTime);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long ttl(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.ttl(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String getSet(String key, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.getSet(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long setnx(String key, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.setnx(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String setex(String key, int seconds, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.setex(key, seconds, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long decrBy(String key, long integer) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.decrBy(key, integer);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long decr(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.decr(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long incrBy(String key, long integer) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.incrBy(key, integer);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long incr(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.incr(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long append(String key, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.append(key, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String substr(String key, int start, int end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.substr(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	// set api
	public Long hset(String key, String field, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hset(key, field, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}
	
	// set api
	public Long hsetExDomain(String key, String field, String value) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hset(key, field, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String hget(String key, String field) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hget(key, field);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long hsetnx(String key, String field, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hsetnx(key, field, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String hmset(String key, Map<String, String> hash) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hmset(key, hash);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public List<String> hmget(String key, String... fields) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hmget(key, fields);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long hincrBy(String key, String field, long value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hincrBy(key, field, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Boolean hexists(String key, String field) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hexists(key, field);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long del(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.del(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long del(String... key) {
		JedisPool pool = getJedisPool(key[0]);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.del(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long hdel(String key, String... fields) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hdel(key, fields);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long hlen(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hlen(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> hkeys(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hkeys(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public List<String> hvals(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hvals(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Map<String, String> hgetAll(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hgetAll(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	// list api
	public Long rpush(String key, String... strings) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.rpush(key, strings);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long lpush(String key, String... strings) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lpush(key, strings);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long lpushx(String key, String string) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lpushx(key, string);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long rpushx(String key, String string) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.rpushx(key, string);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long llen(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.llen(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public List<String> lrange(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lrange(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String ltrim(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.ltrim(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String lindex(String key, long index) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lindex(key, index);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String lset(String key, long index, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lset(key, index, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long lrem(String key, long count, String value) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lrem(key, count, value);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String lpop(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.lpop(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String rpop(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.rpop(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	// set api
	public Long sadd(String key, String... members) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.sadd(key, members);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> smembers(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.smembers(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long srem(String key, String... members) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.srem(key, members);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String spop(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.spop(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long scard(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.scard(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Boolean sismember(String key, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.sismember(key, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public boolean[] sismemberPipeline(String key, String... members) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			Pipeline pipeline = jedis.pipelined();
			for (String member : members) {
				pipeline.sismember(key, member);
			}
			List<Object> retObjects = pipeline.syncAndReturnAll();
			boolean[] ret = new boolean[retObjects.size()];
			for (int i = 0; i < retObjects.size(); i++) {
				ret[i] = (Boolean) retObjects.get(i);
			}
			return ret;
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String srandmember(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.srandmember(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	// sort set api
	public Long zcard(String key) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zcard(key);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long zadd(String key, double score, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zadd(key, score, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long zadd(String key, Map<Double, String> scoreMembers) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zadd(key, scoreMembers);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> zrange(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrange(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Double zincrby(String key, double score, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zincrby(key, score, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public String zfirst(String key) {
		Set<String> res = zrange(key, 0L, 0L);
		if (res == null || res.isEmpty()) {
			return null;
		}
		for (String s : res) {
			return s;
		}
		return null;
	}

	public String zget(String key, Long rank) {
		Set<String> res = zrange(key, rank, rank);
		if (res == null || res.isEmpty()) {
			return null;
		}
		for (String s : res) {
			return s;
		}
		return null;
	}

	public Long zrem(String key, String... members) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrem(key, members);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long zremrangebyrank(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zremrangeByRank(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}

	}

	public Long zrank(String key, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrank(key, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Long zrevrank(String key, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrevrank(key, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> zrevrange(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrevrange(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<Tuple> zrangeWithScores(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrangeWithScores(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrevrangeWithScores(key, start, end);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> zrangeByScore(final String key, final double min, final double max) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrangeByScore(key, min, max);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> zrevrangeByScore(final String key, final double min, final double max) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zrevrangeByScore(key, min, max);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Double zscore(String key, String member) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zscore(key, member);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Double[] zscorePipeline(String key, String... members) {
		JedisPool pool = getJedisPool(key);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			Pipeline pipeline = jedis.pipelined();
			for (String member : members) {
				pipeline.zscore(key, member);
			}
			List<Object> retObjects = pipeline.syncAndReturnAll();
			Double[] ret = new Double[retObjects.size()];
			for (int i = 0; i < retObjects.size(); i++) {
				Object obj = retObjects.get(i);
				if (obj != null)
					ret[i] = (Double) obj;
			}
			return ret;
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	/**
	 * union a list of sort map. result elements will be save in dstkey
	 *
	 * @param dstkye
	 * @param sets
	 * @return the number of elements in the resulting set at destination
	 */
	public Long zunionstore(String dstkye, String... sets) {
		JedisPool pool = getJedisPool(dstkye);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zunionstore(dstkye, sets);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	/**
	 * intersect a list of sort map. result elements will be saved in dstkey
	 *
	 * @param dstkey
	 * @param sets
	 * @return the number of elements in the resulting set at destination
	 */
	public Long zinterstore(String dstkey, String... sets) {
		JedisPool pool = getJedisPool(dstkey);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.zinterstore(dstkey, sets);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public Set<String> keys(final String prefixPattern) {
		JedisPool pool = getJedisPool(prefixPattern);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.keys(prefixPattern);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	public List<Response<String>> hmsetPipeline(final List<Pair<String, Map<String, String>>> kvList) {
		JedisPool pool = getJedisPool(kvList.get(0).getFirst());
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		List<Response<String>> ret = new LinkedList<Response<String>>();
		try {
			Pipeline pipeline = jedis.pipelined();
			for (Pair<String, Map<String, String>> kv : kvList) {
				ret.add(pipeline.hmset(kv.getFirst(), kv.getSecond()));
			}
			pipeline.sync();
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
		return ret;

	}

	/**
	 * 批量获取redis中的信息
	 *
	 * @param fieldList
	 *            map对象中的field
	 * @param key
	 *            redis中的key
	 * @return
	 */
	public List<Pair<String, String>> hmgetPipeline(final String key, final List<String> fieldList) {
		JedisPool pool = getJedisPool(key);
		if (pool == null) {
			LOG.error("pool is null,key is " + key);
			return null;
		}
		Jedis jedis = pool.getResource();
		List<Response<String>> tmpRet = new LinkedList<Response<String>>();
		long sTime = System.currentTimeMillis();
		long eTime = 0;
		try {
			Pipeline pipeline = jedis.pipelined();
			for (String field : fieldList) {
				tmpRet.add(pipeline.hget(key, field));
			}
			pipeline.sync();
			eTime = System.currentTimeMillis();
		} catch (Exception e) {
			eTime = System.currentTimeMillis();
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
			if (eTime - sTime > 10000) {
				LOG.info("hmgetPipeline time: " + (eTime - sTime) + "ms");
			}
		}
		List<Pair<String, String>> ret = new LinkedList<Pair<String, String>>();
		for (int i = 0; i < fieldList.size(); i++) {
			ret.add(new Pair<String, String>(fieldList.get(i), tmpRet.get(i).get()));
		}
		return ret;

	}

	/**
	 * 查询topDomain的某些属性 topDomain在数据库中不存在返回null
	 *
	 * @param topDomain
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> getSiteInfo(String topDomain, String... infoTypes) {
		Map<String, String> returnInfor = null;
		String jsonObj = hget(TOP_DOMAIN_REDIS_KEY, topDomain);
		if (jsonObj != null) {
			Map<String, Object> topDomainInfo = JsonUtil.readObject(jsonObj, HashMap.class);
			if (topDomainInfo != null) {
				returnInfor = new HashMap<String, String>();
				for (String infoType : infoTypes) {
					if (topDomainInfo.containsKey(infoType)) {
						if (topDomainInfo.get(infoType) != null && !topDomainInfo.get(infoType).equals("null")) {
							try {
								returnInfor.put(infoType, (String) topDomainInfo.get(infoType));
							} catch (Exception e) {
								returnInfor.put(infoType, Integer.toString((Integer) topDomainInfo.get(infoType)));

							}
						} else
							returnInfor.put(infoType, "");
					} else
						returnInfor.put(infoType, "");
				}
			}
		}
		return returnInfor;
	}

	@SuppressWarnings("unchecked")
	public Map<String, Map<String, String>> getAllDomainInfos() {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
		try {
			long start = System.currentTimeMillis();
			Map<String, String> ret = jedis.hgetAll(EX_DOMAIN_REDIS_KEY);
			long end = System.currentTimeMillis();
			System.out.println("getAllSiteInfo cost time :" + (end - start));
			for (String domain : ret.keySet()) {
				Map<String, String> domainInfo = JsonUtil.readObject(ret.get(domain), HashMap.class);
				result.put(domain, domainInfo);
			}
			return result;
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

	/**
	 * 批量查询topDomain的某些属性 topDomain在数据库中不存在返回空map
	 *
	 * @param topDomains
	 *            domain list
	 * @param infoTypes
	 *            需要查询的domain 字段
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Pair<String, Map<String, String>>> getSiteInfoPipLine(List<String> topDomains, String... infoTypes) {
		List<Pair<String, String>> topDomainInfos = hmgetPipeline(TOP_DOMAIN_REDIS_KEY,
				topDomains);
		List<Pair<String, Map<String, String>>> topDomainInfoLst = new LinkedList<Pair<String, Map<String, String>>>();
		// topDomainKV - key : top domain - value : json
		for (Pair<String, String> topDomainKV : topDomainInfos) {
			String jsonObj = topDomainKV.getSecond();
			String topDomain = topDomainKV.getFirst();
			Map<String, String> returnInfor = new HashMap<String, String>();
			for (String infoType : infoTypes) {
				returnInfor.put(infoType, "");
			}

			if (jsonObj != null) {
				Map<String, Object> tmpTopDomainInfo = JsonUtil.readObject(jsonObj, HashMap.class);
				if (tmpTopDomainInfo != null) {
					for (String infoType : infoTypes) {
						if (tmpTopDomainInfo.containsKey(infoType)) {
							if (tmpTopDomainInfo.get(infoType) != null
									&& !tmpTopDomainInfo.get(infoType).equals("null")) {
								try {
									returnInfor.put(infoType, (String) tmpTopDomainInfo.get(infoType));
								} catch (Exception e) {
									returnInfor.put(infoType,
											Integer.toString((Integer) tmpTopDomainInfo.get(infoType)));
								}
							}
						}
					}
				}
			}
			topDomainInfoLst.add(new Pair<String, Map<String, String>>(topDomain, returnInfor));
		}
		return topDomainInfoLst;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> getAllExDomain(String redisKey) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		List<Map<String, String>> lstReturn = new ArrayList<Map<String, String>>();
		if (pool == null) {
			return lstReturn;
		}
		Jedis jedis = pool.getResource();

		try {
			List<String> lstValue = null;
			Map<String, String> map = null;
			Set<String> hkeys = jedis.hkeys(redisKey);
			List<String> tmp = new ArrayList<String>();
			int bluk = 500;
			for (String key : hkeys) {
				if (!tmp.isEmpty() && tmp.size() % bluk == 0) {
					String[] fields = tmp.toArray(new String[0]);
					lstValue = jedis.hmget(redisKey, fields);
					for (String jsonObj : lstValue) {
						if (jsonObj != null) {
							try {
								map = JsonUtil.readObject(jsonObj, HashMap.class);
							} catch (Exception e) {
								LOG.error("----------------------jsonObj size:"+jsonObj.length()+"jsonObj:"+jsonObj.substring(jsonObj.length()-100, jsonObj.length()),e);
							}
						}
					}
					tmp.clear();
					lstValue.clear();
					map.clear();
				}
				tmp.add(key);
			}

			if (!tmp.isEmpty()) {
				String[] fields = tmp.toArray(new String[0]);
				lstValue = jedis.hmget(redisKey, fields);
				for (String jsonObj : lstValue) {
					if (jsonObj != null) {
						map = JsonUtil.readObject(jsonObj, HashMap.class);
						if (map != null) {
							lstReturn.add(map);
						}
					}
				}
				tmp.clear();
				lstValue.clear();
				map.clear();
			}

			/*
			 * List<String> lstValue = jedis.hvals(redisKey); Map<String,
			 * String> map = null; for (String jsonObj : lstValue) { if(jsonObj
			 * != null){ map = JsonUtil.readObject(jsonObj, HashMap.class); if
			 * (map != null) { lstReturn.add(map); } } }
			 */
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
		} finally {
			pool.returnResource(jedis);
		}

		return lstReturn;
	}

	public Map<String,ExDomainRegex> getAllExDomainMap(String redisKey) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		Map<String,ExDomainRegex> map =new HashMap<String, ExDomainRegex>();
		if (pool == null) {
			return map;
		}
		Jedis jedis = pool.getResource();
		ExDomainRegex exDomainRegex = null;
		try {//redisKey=EXDOMAIN
			 Map<String, String> all = jedis.hgetAll(redisKey);
			 for (String key : all.keySet()) {
				 // hset EXDOMAIN  22.com "{\"IPS\":\"111.47.3.0,\",\"SUFFIX\":\"22.com\",\"DOMAIN\":\"www.22.com\",\"FINDTIME\":\"20170626\"}"
				 String jsonObj =all.get(key);//第二key 22.com
				 
				 exDomainRegex = str2Bean(jsonObj);
				 if(exDomainRegex!=null){
					 map.put(key, exDomainRegex);
				 }
			}
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("get all exdomain error ......", e);
		} finally {
			pool.returnResource(jedis);
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	ExDomainRegex str2Bean(String jsonObj){
		Map<String, String> tmp = null;
		ExDomainRegex exDomainRegex = null;
		if (jsonObj != null) {
			try {   //从json串转换成HashMap实体对象 
				tmp = JsonUtil.readObject(jsonObj, HashMap.class);
				if (tmp != null) {
					//获得SUFFIX的值
					String suffix = tmp.get(EX_DOMAIN_SUFFIX_KEY);
					//获得DOMAIN的值
					String domain = tmp.get(EX_DOMAIN_NAME_KEY);
					//获得IPS的值
					String ips = tmp.get(EX_DOMAIN_IPS_KEY);
					if (suffix != null && domain != null && ips != null) {
						String[] ipArr = ips.split(",");
						Set<String> ipSet = new HashSet<String>();
						for (String ip : ipArr) {
							if(ipSet.size()>maxLimit){//10000
								LOG.error("----------------------ipSet size too long ,suffix:"+suffix+" -------------domain:"+domain);
								break;
							}
							ipSet.add(ip);
						}
						
						if (!ipSet.isEmpty()) {
							exDomainRegex = new ExDomainRegex(suffix, domain, ipSet);
						}
						
					}
				}
			} catch (Exception e) {
				LOG.error("----------------------jsonObj size:"+jsonObj.length()+" jsonObj:"+jsonObj,e);
			}
		}
		return exDomainRegex;
	}
	
	
	public List<ExDomainRegex> getAllExDomains(String redisKey) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		List<ExDomainRegex> lstReturn = new ArrayList<ExDomainRegex>();
		if (pool == null) {
			return lstReturn;
		}
		Jedis jedis = pool.getResource();

		try {
			Set<String> hkeys = jedis.hkeys(redisKey);
			List<String> tmp = new ArrayList<String>();
			int bluk = 500;
			for (String key : hkeys) {
				if (!tmp.isEmpty() && tmp.size() % bluk == 0) {
					lstReturn.addAll(parse(jedis, redisKey, tmp));
					tmp.clear();
				}
				tmp.add(key);
			}

			if (!tmp.isEmpty()) {
				lstReturn.addAll(parse(jedis, redisKey, tmp));
			}

		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
		} finally {
			pool.returnResource(jedis);
		}
		return lstReturn;
	}
	

	@SuppressWarnings("unchecked")
	public List<ExDomainRegex> parse(Jedis jedis, String redisKey, List<String> tmp) {
		List<ExDomainRegex> lstReturn = new ArrayList<ExDomainRegex>();
		String[] fields = tmp.toArray(new String[0]);
		List<String> lstValue = jedis.hmget(redisKey, fields);
		Map<String, String> map = null;
		ExDomainRegex exDomainRegex = null;
		for (String jsonObj : lstValue) {
			if (jsonObj != null) {
				try {
					map = JsonUtil.readObject(jsonObj, HashMap.class);
				} catch (Exception e) {
					LOG.error("----------------------jsonObj size:"+jsonObj.length()+" jsonObj:"+jsonObj,e);
				}
				
				if (map != null) {
					String suffix = map.get(EX_DOMAIN_SUFFIX_KEY);
					String domain = map.get(EX_DOMAIN_NAME_KEY);
					String ips = map.get(EX_DOMAIN_IPS_KEY);
					if (suffix != null && domain != null && ips != null) {
						String[] ipArr = ips.split(",");
						Set<String> ipSet = new HashSet<String>();
						for (String ip : ipArr) {
							if(ipSet.size()>maxLimit){
								LOG.error("----------------------ipSet size >10000,suffix:"+suffix+" -------------domain:"+domain);
								break;
							}
							ipSet.add(ip);
						}
						if (!ipSet.isEmpty()) {
							exDomainRegex = new ExDomainRegex(suffix, domain, ipSet);
							lstReturn.add(exDomainRegex);
						}
					}
				}
			}
		}
		return lstReturn;
	}

	public Set<String> getAllExDomainKeys(String redisKey) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		/*
		 * List<Map<String,String>> lstReturn = new
		 * ArrayList<Map<String,String>>();
		 */
		Set<String> set = new HashSet<String>();
		if (pool == null) {
			return set;
		}
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			set = jedis.hkeys(redisKey);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
		} finally {
			pool.returnResource(jedis);
		}
		return set;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> getExDomainByFields(String redisKey, Set<String> fieldKeys) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		List<Map<String, String>> lstReturn = new ArrayList<Map<String, String>>();
		if (pool == null) {
			return lstReturn;
		}
		Jedis jedis = null;
		try {
			String[] fields = fieldKeys.toArray(new String[0]);
			jedis = pool.getResource();
			List<String> lstValue = jedis.hmget(redisKey, fields);
			Map<String, String> map = null;
			for (String jsonObj : lstValue) {
				if (jsonObj != null) {
					map = JsonUtil.readObject(jsonObj, HashMap.class);
					if (map != null) {
						lstReturn.add(map);
					}
				}
			}
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
		} finally {
			pool.returnResource(jedis);
		}
		return lstReturn;
	}
	public Map<String,String>  getAllExDomainCache(String redisKey) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		if (pool == null) {
			return new HashMap<String, String>();
		}
		Jedis jedis = pool.getResource();

		try {
			return jedis.hgetAll(redisKey);
			
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("", e);
		} finally {
			pool.returnResource(jedis);
		}
		return new HashMap<String, String>();
	}
	public String hsetExDomains(String key, Map<String, String> values) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hmset(key, values);
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error("hmset key: "+key+"  error  ", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}
	public Long hDelExDomains(String key, List<String> list) {
		JedisPool pool = getJedisPool(TOP_DOMAIN_REDIS_KEY);
		if (pool == null)
			return null;
		Jedis jedis = pool.getResource();
		try {
			return jedis.hdel(key, list.toArray(new String[list.size()]));
		} catch (Exception e) {
			pool.returnBrokenResource(jedis);
			LOG.error(" batch hdel key: "+key+"  error  ", e);
			return null;
		} finally {
			pool.returnResource(jedis);
		}
	}

}
